package _13二分查找;

import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        int[] arr={6,1,2,7,9,3,4,5,10,8};
        //二分查找的前提条件时数组是有序的
        Arrays.sort(arr);
        Boolean haveNum = isHaveNum(arr, 5);
        System.out.println(haveNum);
    }

    public static Boolean isHaveNum(int[] arr,int target){
        int minIndex=0;
        int maxIndex=arr.length-1;

        while (minIndex<=maxIndex){
            int midIndex = (maxIndex-minIndex)/2;

            if (target == arr[midIndex]){

                return true;
            }else if (target > arr[midIndex]){
                //目标值大于中间值，中间值左边的数据可直接排除
                minIndex = midIndex+1;

            }else if (target<arr[maxIndex]){
                //目标值小于中间值，中间值右边的数据排除
                maxIndex = midIndex-1;
            }
        }

        return false;


    }
}
